<?php

class Photo extends Controller {

	function Photo()
	{
		parent::Controller();
		if(!$this->session->userdata('logged_in')) {
			redirect('app/login');
		}
		$this->load->model('Photo_model');
		$this->load->model('Category_model');
	}
	
	function index(){
		$this->main($this->Category_model->get_first());
	}
	
	function main($category_id)
	{
		$data['categories'] = $this->Category_model->get_all();
		$data['photos'] = $this->Photo_model->get_all_by_category($category_id);
		$data['category'] = $this->Category_model->get_category($category_id);
		$this->load->view('photo/index.php',$data);
	}
	
	function create()
	{
		$data['categories'] = $this->Category_model->get_all();
		$this->load->view('photo/create.php',$data);
	}
	
	function edit()
	{
		$this->load->view('photo/edit.php');
	}
	
	function insert()
	{
		if($this->Photo_model->upload()){
			if(!$this->Photo_model->insert()){
				$this->session->set_flashdata('error', TRUE);
			}
		}
		else{
			$this->session->set_flashdata('error', TRUE);
		}
		redirect('photo/main/'.$this->input->post('category_id'));
	}
	
	function change_category()
	{
		if(!$this->Photo_model->set_category_id()){
			set_ajax_status('1');
			echo $this->session->flashdata('msg');
		}
		else{
			set_ajax_status('0');
			echo 'ok';
		}
	}
	
	function change_position()
	{
		if(!$this->Photo_model->set_position()){
			set_ajax_status('1');
			echo $this->session->flashdata('msg');
		}
		else{
			set_ajax_status('0');
			echo 'ok';
		}
	}
	
	function delete(){
		echo "alert('hola');";
		/*if(!$this->Photo_model->delete()){
			$this->session->set_flashdata('error', TRUE);
		}*/
	}

}
?>